﻿#nullable enable

using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;
using PHMEE.Localization;
using Volo.Abp;
using Volo.Abp.Domain.Services;

namespace PHMEE.EnergyEfficiency;

public class VesselEeConfigDomainService : DomainService
{
    private readonly IStringLocalizer<EnergyEfficiencyResource> _localizer;
    private readonly IVesselEeConfigRepository _vesselEeConfigRepository;

    public VesselEeConfigDomainService(IStringLocalizer<EnergyEfficiencyResource> localizer, 
        IVesselEeConfigRepository vesselEeConfigRepository)
    {
        _localizer = localizer;
        _vesselEeConfigRepository = vesselEeConfigRepository;
    }


    public async Task<VesselEeConfig> GetByVesselIdAsync(Guid vesselId)
    {
        var vesselEeConfig = await _vesselEeConfigRepository.GetByVesselIdAsync(vesselId);
        if (vesselEeConfig is null)
        {
            throw new UserFriendlyException(_localizer[PHMEEEnergyEfficiencyErrorCodes.VesselConfigNotFound],
                PHMEEEnergyEfficiencyErrorCodes.VesselConfigNotFound);
        }
        
        return vesselEeConfig;
    }

    public Task<VesselEeConfig?> GetOrDefaultByVesselIdAsync(Guid vesselId)
    {
        return _vesselEeConfigRepository.GetByVesselIdAsync(vesselId);
    }
    
    /// <summary>
    /// 获取 Capacity，可能为 0
    /// </summary>
    /// <param name="vesselType"></param>
    /// <param name="dwt"></param>
    /// <param name="gt"></param>
    /// <returns></returns>
    public static float GetCapacity(VesselType vesselType, float dwt, float gt)
    {
        var ciiBaselineParameters = VesselTypeHelper.GetCIIBaselineParameters(vesselType, dwt, gt);

        return ciiBaselineParameters.Capacity;
    }
}